#include <stdlib.h>

#include "lcd_BSP.h"
#include "INA226\INA226.h"
#include "rtthread.h"
#include "main.h"

#define COLOR_V RED
#define COLOR_I YELLOW
#define COLOR_P LIGHTBLUE

// #define COLOR_V    0x6555
// #define COLOR_I    0xFF71
// #define COLOR_P    0x4416
#define COLOR_BAR    GRAY
#define COLOR_BASE   BLACK

#define X_Star       4
#define BAR_W        128
#define BAR_H        1
#define Font_H       24
#define DEBUG_Font_H 12

enum Custom_Titlle_Type {
    Power,
    Energy,
    Voltage_Max,
    Current_Max,
    Power_Max,
};

// 定义状态枚举类型
enum Custom_Page_Type {
    RealTime,
    MaxValue,
    Waveform,
    Debugform,
};

// 定义方向枚举类型

struct Page {
    enum Custom_Page_Type pageNumber; // 页码
    unsigned char direction;          // 方向
    unsigned char color;              // 颜色
};

struct Page myPage = {
    .pageNumber = RealTime,
    .direction  = USE_HORIZONTAL,
    .color      = COLOR_BASE};

uint16_t RealTime_V, RealTime_I;
uint32_t RealTime_P;

uint8_t Custom_Titlle = Power;
float R_shunt         = 10.0f; // mR

void RealTimeUI_Init(void);
void WaveformUI_Init(void);
void Debug_Init(void);

void MaxValueUI_Display(void);
void WaveformUI_Display(void);
void RealTimeUI_Display(void);
void Debug_Display(void);

void Voltage_Title(int startY);
void Current_Title(int startY);
void Power_Title(int startY);

void UI_Dir_Toggle(void);

// 初始化和显示函数指针数组
void (*InitFuncs[])(void) = {
    [RealTime]  = RealTimeUI_Init,
    [Waveform]  = WaveformUI_Init,
    [Debugform] = Debug_Init};

void (*DisplayFuncs[])(void) = {
    [RealTime]  = RealTimeUI_Display,
    [Waveform]  = WaveformUI_Display,
    [Debugform] = Debug_Display};

void LCD_UI_Update(void)
{
    static int Page_Last = -1;

    // 更新方向
    if (myPage.direction != USE_Direction) {
        USE_Direction = myPage.direction;
        LCD_Init();
        InitFuncs[myPage.pageNumber]();
    }

    // 更新页面内容
    if (myPage.pageNumber != Page_Last) {
        Page_Last = myPage.pageNumber;

        // 调用初始化函数
        if (myPage.pageNumber >= RealTime && myPage.pageNumber <= Debugform) {
            InitFuncs[myPage.pageNumber]();
        }
    }

    // 调用显示函数
    if (myPage.pageNumber >= RealTime && myPage.pageNumber <= Debugform) {
        DisplayFuncs[myPage.pageNumber]();
    }
}

void UI_Dir_Toggle(void)
{
    switch (myPage.direction) {
        case 0:
            myPage.direction = 1;
            break;
        case 1:
            myPage.direction = 0;
            break;
        case 2:
            myPage.direction = 3;
            break;
        case 3:
            myPage.direction = 2;
            break;

        default:
            myPage.direction = 0;
            break;
    }
}

void MaxValueUI_Init(void)
{
}
void MaxValueUI_Display(void)
{
}

void WaveformUI_Init(void)
{
}
void WaveformUI_Display(void)
{
}

void RealTimeUI_Init(void)
{
    unsigned char Tittle_H = Font_H + BAR_H + 2;
    LCD_Fill(0, 0, LCD_W, LCD_H, COLOR_BASE);
    LCD_ShowString(X_Star, Tittle_H * 0, " 00.000 V", COLOR_V, COLOR_BASE, Font_H);
    LCD_ShowString(X_Star, Tittle_H * 1, " 00.000 A", COLOR_I, COLOR_BASE, Font_H);
    LCD_ShowString(X_Star, Tittle_H * 2, " 000.00 W", COLOR_P, COLOR_BASE, Font_H);
    LCD_Fill(X_Star, Font_H + Tittle_H * 0 + 1, X_Star + BAR_W, Tittle_H * 1 - 1, GRAY);
    LCD_Fill(X_Star, Font_H + Tittle_H * 1 + 1, X_Star + BAR_W, Tittle_H * 2 - 1, GRAY);
    LCD_Fill(X_Star, Font_H + Tittle_H * 2 + 1, X_Star + BAR_W, Tittle_H * 3 - 1, GRAY);
}
void RealTimeUI_Display(void)
{
    unsigned char Tittle_H = Font_H + BAR_H + 2;

    // 调用电压、电流和功率显示函数
    Voltage_Title(Tittle_H * 0);
    Current_Title(Tittle_H * 1);
    Power_Title(Tittle_H * 2);
}

void Debug_Init(void)
{
    LCD_Fill(0, 0, LCD_W, LCD_H, COLOR_BASE);
}

void Debug_Display(void)
{
    char LCD_Temp[30];
    rt_sprintf(LCD_Temp, "0x01: %6d %3d.%03dmV", sensor_data.raw_shuntV, (int)sensor_data.shuntV / 1000, abs((int)sensor_data.shuntV % 1000));
    LCD_ShowString(0, (DEBUG_Font_H + 1) * 1, LCD_Temp, ~COLOR_BASE, COLOR_BASE, DEBUG_Font_H);
    rt_sprintf(LCD_Temp, "0x02:  %5d   %5dmV", sensor_data.raw_voltage, sensor_data.voltage);
    LCD_ShowString(0, (DEBUG_Font_H + 1) * 2, LCD_Temp, ~COLOR_BASE, COLOR_BASE, DEBUG_Font_H);
    rt_sprintf(LCD_Temp, "0x03:  %5d  %6dmW", sensor_data.raw_power, sensor_data.power);
    LCD_ShowString(0, (DEBUG_Font_H + 1) * 3, LCD_Temp, ~COLOR_BASE, COLOR_BASE, DEBUG_Font_H);
    rt_sprintf(LCD_Temp, "0x04:  %5d   %5dmA", sensor_data.raw_current, sensor_data.current);
    LCD_ShowString(0, (DEBUG_Font_H + 1) * 4, LCD_Temp, ~COLOR_BASE, COLOR_BASE, DEBUG_Font_H);
    rt_memset(LCD_Temp, 0, 30);
    rt_sprintf(LCD_Temp, "Cont: %c%5d.%03d mAh", (sensor_data.energy > 0 ? ' ' : '-'), abs((int)sensor_data.energy / 1000), abs((int)sensor_data.energy % 1000));
    LCD_ShowString(0, (DEBUG_Font_H + 1) * 5, LCD_Temp, ~COLOR_BASE, COLOR_BASE, DEBUG_Font_H);
}

// 显示电压的函数
void Voltage_Title(int startY)
{
    char LCD_Temp[10];
    static uint16_t raw_voltage_last = 0;

    if (raw_voltage_last != sensor_data.raw_voltage) {
        raw_voltage_last = sensor_data.raw_voltage;
        LCD_Fill(X_Star, startY + Font_H + 1, X_Star + BAR_W, startY + Font_H + 1 + BAR_H, GRAY);
        LCD_Fill(X_Star, startY + Font_H + 1, X_Star + (sensor_data.raw_voltage * BAR_W / 0x8000), startY + Font_H + 1 + BAR_H, COLOR_V);
        rt_sprintf(LCD_Temp, " %2d.%03d", sensor_data.voltage / 1000, sensor_data.voltage % 1000);
        LCD_ShowString(X_Star, startY, LCD_Temp, COLOR_V, COLOR_BASE, Font_H);
    }
}

// 显示电流的函数
void Current_Title(int startY)
{
    char LCD_Temp[10], *arrow;
    static int16_t raw_current_last = 0;
    if (raw_current_last != sensor_data.raw_current) {
        raw_current_last = sensor_data.raw_current;
        LCD_Fill(X_Star, startY + Font_H + 1, X_Star + BAR_W, startY + Font_H + 1 + BAR_H, GRAY);
        LCD_Fill(X_Star, startY + Font_H + 1, X_Star + (abs(sensor_data.raw_current) * BAR_W / 0x8000), startY + Font_H + 1 + BAR_H, COLOR_I);
        rt_sprintf(LCD_Temp, " %2d.%03d", abs(sensor_data.current / 1000), abs(sensor_data.current % 1000));
        LCD_ShowString(X_Star, startY, LCD_Temp, COLOR_I, COLOR_BASE, Font_H);
        if (sensor_data.current > 0) {
            arrow = (myPage.direction == USE_HORIZONTAL) ? "<<" : ">>";
        } else if (sensor_data.current == 0) {
            arrow = "  ";
        } else {
            arrow = (myPage.direction == USE_HORIZONTAL) ? ">>" : "<<";
        }

        LCD_ShowString(X_Star + BAR_W, startY, arrow, COLOR_I, COLOR_BASE, Font_H);
    }
}

// 显示功率的函数
void Power_Title(int startY)
{
    char LCD_Temp[10];

    static uint16_t raw_power_last = 0;

    if (raw_power_last != sensor_data.raw_power) {
        raw_power_last = sensor_data.raw_power;
        LCD_Fill(X_Star, startY + Font_H + 1, X_Star + BAR_W, startY + Font_H + 1 + BAR_H, GRAY);
        LCD_Fill(X_Star, startY + Font_H + 1, X_Star + (sensor_data.raw_power * BAR_W / 0x8000), startY + Font_H + 1 + BAR_H, COLOR_P);
        rt_sprintf(LCD_Temp, " %3d.%02d", sensor_data.power / 1000, sensor_data.power % 1000 / 10);
        LCD_ShowString(X_Star, startY, LCD_Temp, COLOR_P, COLOR_BASE, Font_H);
    }
}